package com.smartmore.test;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class ImageTestView1 extends View{
	
	Bitmap bitmap1 = null;
	
	
	List<ImageRectInfo> imageRectList = new ArrayList<ImageRectInfo>();
	
	public ImageTestView1(Context context) {
		super(context);
		init();
	}
	
	public ImageTestView1(Context context, AttributeSet attSet) {
		super(context, attSet);
		init();
	}
	
	public ImageTestView1(Context context, AttributeSet attSet, int defStyle) {
		super(context,attSet,defStyle);
		init();
	}
	
	public void init() {
		bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.oldman);
		setWillNotDraw(false);
		
		imageRectList.add(new ImageRectInfo(0,0, 215, 300));
		imageRectList.add(new ImageRectInfo(215,0, 430, 300));
	}
	
	
	float  downX, downY, curX, curY, movedX,movedY;
	float x,y;
	
	private void setMoveImageRectInfo(float x, float y) {
		for(int i=0; i<imageRectList.size(); i++) {
			ImageRectInfo ir = (ImageRectInfo)imageRectList.get(i);
			Rect rect = ir.getRect();
			if(rect.contains((int)x, (int)y)) {
				ir.setMoveable(true);
			}else {
				ir.setMoveable(false);
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {			
		
		 switch (event.getAction()) {
		 	
	         case MotionEvent.ACTION_DOWN:
	             downX = event.getX();
	             downY = event.getY();
	             setMoveImageRectInfo(downX,downY);
	             
	             break;
	         case MotionEvent.ACTION_MOVE:
	             curX = event.getX();
	             curY = event.getY();
	             break;
	         case MotionEvent.ACTION_UP:
                 movedX = (int) (  downX -curX);
                 movedY= (int) (downY-curY);
                 Log.d("x","==>"+(int) (movedX));
                 invalidate();
                 
                 downX = curX;
                 downY = curY;
                 break;
                 
		 }
		
		return true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		
		
		Paint paint = new Paint();		
		paint.setStyle(Paint.Style.STROKE); 
		paint.setColor(Color.WHITE);
		
		//canvas.drawBitmap(bitmap1,-600,0, null);		
		for(int i=0; i<imageRectList.size(); i++) {
			ImageRectInfo ir = (ImageRectInfo)imageRectList.get(i);
			
						
			//canvas.drawBitmap(bitmap1,new Rect((int)(x+ir.left),(int)ir.top,(int)(ir.width+x),300),new RectF(ir.left,ir.top,ir.width,300), null);
			canvas.drawBitmap(bitmap1,new Rect((int)(x),(int)0,(int)(ir.width-ir.left),300),new RectF(ir.left,ir.top,ir.width,300), null);
			
//			if(ir.isMoveable()) {
//				x += movedX;			
//				Log.d("x",i+"==>"+"onDraw:"+movedX+","+x);
//				//canvas.drawBitmap(bitmap1,null,new RectF(x+ir.left,ir.top,ir.width+x,300), null);
//				canvas.drawBitmap(bitmap1,new Rect((int)(x+ir.left),(int)ir.top,(int)(ir.width+x),300),new RectF(ir.left,ir.top,ir.width,300), null);
//			}else {
//				canvas.drawBitmap(bitmap1,new Rect((int)(x+ir.left),(int)ir.top,(int)(ir.width+x),300),new RectF(ir.left,ir.top,ir.width,300), null);				
//			}
			canvas.drawRect(ir.getRect(),paint) ;
		}
		
		/*
		canvas.drawBitmap(bitmap1,null,new RectF(x,0,215+x,300), null);		
		canvas.drawBitmap(bitmap1,null,new RectF(215+x,0,430+x,300), null);
		*/
		
		super.onDraw(canvas);
	}
	
	
	public class ImageRectInfo {
		
		
		float left;
		float top;
		float width;
		float height;
		Rect rect ;
		boolean isMoveable =false;
		
		public ImageRectInfo(float l,float t, float w, float h) {
			this.left = l;
			this.top = t;
			this.width = w;
			this.height = h;
		}
		
		public boolean isMoveable() {
			return isMoveable;
		}
		
		public void setMoveable(boolean m) {
			this.isMoveable = m;
		}
		
		public Rect getRect() {
			return new Rect((int)left, (int)top, (int)width, (int)height);
		}
	}
	
}
